<?php
namespace app\index\model;

use think\Db;
class UserModel extends BaseModel
{
    protected $table = 'user';

    /**
     * 通过手机号获取登录信息
     *
     * @param string $mobile 手机
     * 
     * @return void
     */
    public function getUserByMoblie($mobile)
    {
        $where = [
            'mobile' => $mobile,
        ];
        return  Db::name($this->table)
                    ->where($where)
                    ->find();
    }
    
    /**
     * 根据id获取用户
     *
     * @param int $uid 用户id
     * 
     * @return array
     */
    public function baseUser($uid)
    {
        $where = [
            'uid' => $uid,
        ];
        return $this->where($where)->find();
    }
    
    /**
     * 修改密码
     *
     * @param [type] $data
     * @return void
     */
    public function updateUserPwd($data)
    {
        $where = [
            'uid' => $data['uid'],
        ];
        $updateData =  [
                            'password' => $data['password'],
                        ];
        unset($data['uid']);
        $row = $this->where($where)->update($updateData);

        if ($row == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 注册
     *
     * @param string $data 用户数据
     * @param int    $role 用户类型 1=学生
     * 
     * @return void 
     */
    public function reg($user,$info=null)
    {
        $data = [
            'mobile' => $user['mobile'],
            'password' => $user['password'],
        ];
        $this->startTrans();
        $uid = Db::name($this->table)->insertGetId($data);
        if ($uid) {
            
        } else {
            goto err;
        }
        
        $this->commit();
        return $uid;
        err:
        $this->rollback();
        return false;
    }

    /**
     * 用户消息
     *
     * @param string $uid  用户id
     * @param int    $page 页码
     * @param int    $size 页大小
     * 
     * @return void
     */
    public function getMsg($uid, $page, $size)
    {
        $limit = pageper($page, $size);

        // 更新用户最新消息字段
        $where=
        [
            ['uid','=',$uid]
        ];

        $data = 
        [
            'has_new_msg'=>0
        ];
        $row = Db::table($this->table)
                ->where($where)
                ->update($data);

        $limit1 = $limit[0];
        $limit2 = $limit[1];
        return Db::query(
            "select msg_id,title,content,msg_type,created from pract_msg
            where uid = $uid and msg_type != 1 
            union 
            select msg.msg_id,title,content,msg_type,msg.created from pract_msg msg 
            where msg_type = 1 
            and NOT EXISTS (select * from pract_msg_del del 
            where del.uid = $uid and msg.msg_id = del.msg_id)
            order by created desc
            limit $limit1,$limit2"
        );
    }

    /**
     * Undocumented function
     *
     * @param int    $id  消息id
     * @param int    $uid 用户id
     * @param string $msg 错误消息
     * 
     * @return bool
     */
    public function delMsg($id, $uid, &$msg)
    {
        $where = [
            'msg_id' => $id,
        ];
        $msg = Db::table($this->tb_msg)
                ->where($where)
                ->find();
        $where['uid'] = $uid;
        $del = Db::table($this->tb_msg_del)
                    ->where($where)
                    ->find();
        if (!$msg || $del) {
            $msg = '消息不存在或已删除';
            goto err;
        }
        if ($msg['msg_type'] == 1) {
            $row = Db::table($this->tb_msg_del)
                    ->insert($where);
        } else {
            $row = Db::table($this->tb_msg)
                    ->where($where)
                    ->delete();
        }

        if ($row != 1) {
            $msg = '删除失败';
            goto err;
        }
        return true;
        err: 
        return false;
    }

    /**
     * 修改用户信息
     *
     * @param int   $uid  用户id
     * @param int   $role 角色
     * @param array $data 信息
     * 
     * @return bool
     */
    public function modify($uid, $role, $data, $baseInfo,&$msg)
    {
        $where = [
            'uid' => $uid,
        ];
        $baseInfo['updated'] = dateTime();
        $this->startTrans();
        try
        {

            $row = Db::table($this->table)
                        ->where($where)
                        ->update($baseInfo);
                        
            $row1 = Db::table($this->tb_user_info)
                        ->where($where)
                        ->update($data);
                // echo $this->getlastsql();die;
            if ($row != 1) {
                $this->rollback();
                return false;
            }
            $this->commit();
            return true;
        }
        catch(\think\Exception $e)
        {
            $this->rollback();
            $msg = $e->getMessage();
            return false;
        }
    }
}
